const app = getApp();
const utils = require('../../utils/utils.js');
Page({
    data: {
        isClick: true,
        userInfo: {},
        deposit: 0,
        tel_: ""
    },
    onLoad: function (options) {
        // 获取用户信息
        wx.showLoading({
            title: 'loading...',
            mask: true
        });
        this.getUserInfo();
        wx.hideLoading();
    },
    onShow: function () {
        wx.showLoading({
            title: 'loading...',
            mask: true
        });
        this.getUserInfo();
        wx.hideLoading();
    },
    // =============== 获取用户信息 ====================
    getUserInfo() {
        let that = this;
        utils.httpRequest('/getInfo', {}, function (res) {
            if (res.code == 1) {
                that.setData({
                    userInfo: res.data.user,
                    deposit: res.data.deposit,
                    tel_: res.data.tel_
                })
            }
        })
    },
    // ============== 执行退押金操作 =====================
    returnDeposit() {
        let that = this;
        if (that.data.isClick) {
            that.setData({
                isClick: false
            });
            if (that.data.deposit <= 0) {
                wx.showToast({
                    title: "您不存在押金！",
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isClick: true
                })
            } else {
                wx.showModal({
                    title: '退押金提醒',
                    content: '是否退押金？',
                    success: function (sm) {
                        if (sm.confirm) {
                            utils.httpRequest('/returnDeposit', {}, function (res) {
                                that.setData({
                                    isClick: true
                                });
                                if (res.code == 1) {
                                    wx.showToast({
                                        title: "恭喜，退款成功,您的押金预计在1-3个工作日原路退回您的支付账户，请注意查收！",
                                        icon: 'none',
                                        duration: 5000
                                    });
                                    // 重新获取用户信息
                                    that.getUserInfo();
                                }
                            })
                        } else {
                            that.setData({
                                isClick: true
                            });
                        }
                    }
                })
            }
        }
    },
    // 客服电话
    makePhoneCall: function (e) {
        let tel = e.currentTarget.dataset.tel;
        //显示“呼叫”、“添加联系人”弹窗
        wx.showActionSheet({
            itemList: ['呼叫'],
            success: function (res) {
                if (res.tapIndex == 0) {//直接呼叫
                    wx.makePhoneCall({
                        phoneNumber: tel,
                        success: function (res_makephone) {
                        }
                    })
                }
            }
        })
    },
});